<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="txId" placeholder="交易id" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getMessageByTxId()">交易id查询</el-button>
    </el-form>

    <!-- banner列表 -->
    <el-table :data="list" stripe style="width: 100%">
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="blockhash" label="区块hash" />
      <el-table-column prop="contractId" label="合约id" />
      <el-table-column prop="contractName" label="合约名称" />
      <el-table-column prop="blocktime" label="区块时间" />
    </el-table>
  </div>
</template>

<script>
import chain from '@/api/chain'

export default {
  // 定义变量和初始值
  data() {
    return {
      txId: '',
      list: []
    }
  },
  created() {
  },
  methods: {
    getMessageByTxId() {
      chain
        .getMessageByTxId(this.txId)
        .then((response) => {
          const temp = response.data
          temp.blocktime = this.timeConvert(parseInt(temp.blocktime) * 1000)
          const arr = []
          arr.push(temp)
          this.list = arr
          console.log(response)
        }) // 请求成功
        .catch((error) => {
          console.log(error)
        }) // 请求失败
    },
    timeConvert(timestamp) {
      // num:0 YYYY-MM-DD  num:1  YYYY-MM-DD hh:mm:ss // timestamp:时间戳
      // 将时间戳转换成正常时间格式
      var date = new Date(timestamp) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var Y = date.getFullYear() + '-'
      var M =
        (date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1) + '-'
      var D = date.getDate() + ' '
      var h = date.getHours() + ':'
      var m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
      var s = date.getSeconds()
      return Y + M + D + h + m + s
    }
  }
}
</script>
